//
//  YCCommentTableViewCell.m
//  LOLBox
//
//  Created by mac on 15-3-24.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCCommentTableViewCell.h"
#import "YCCommentAccout.h"
#import "UIImageView+WebCache.h"
#import "YCReplyCommentAccout.h"

@interface YCCommentTableViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *username;
@property (weak, nonatomic) IBOutlet UILabel *content;
@property (weak, nonatomic) IBOutlet UILabel *postTime;

@property (weak, nonatomic) IBOutlet UIView *repView;
@property (weak, nonatomic) IBOutlet UILabel *repUsername;
@property (weak, nonatomic) IBOutlet UILabel *repContent;

@end

@implementation YCCommentTableViewCell

- (void)awakeFromNib
{
    // Initialization code
//    self.content.frame = CGRectMake(63, 27, 251, 21);
//    self.repContent.frame = CGRectMake(5, 24, 238, 39);
    self.icon.layer.cornerRadius = 5;
    self.icon.clipsToBounds = YES;
    self.repView.layer.cornerRadius = 5;
    self.repView.clipsToBounds = YES;
}
//填充数据
- (void)fillCellWithModel:(YCCommentAccout *)accout
{
    //计算content高度
    CGFloat conH = [self setRowHeight:accout.content];
    CGFloat repConH = 0;
    
    [self.icon sd_setImageWithURL:[NSURL URLWithString:accout.icon] placeholderImage:[UIImage imageNamed:@"noimage_xiangqing"]];
//    self.username.text = [NSString stringWithFormat:@"%@ %@",accout.username,accout.floor];
    self.username.attributedText = [self changetextFont:accout.username WithFont:[NSString stringWithFormat:@"%@",accout.floor]];
    
    self.content.text = accout.content;
    
    NSString *s = (NSString *)accout.postTime;
    self.postTime.text = [self intervalSinceNow:s];
    
    if (accout.replyComment == nil)
    {
        //NSLog(@"美术");
        self.repView.hidden = YES;
        self.repUsername.hidden = YES;
    }
    else
    {
        self.repView.hidden = NO;
        self.repUsername.hidden = NO;
        //NSLog(@"---有数据");
        YCReplyCommentAccout *rep =accout.replyComment;
        NSString *name;
        if (![rep.username isKindOfClass:[NSNull class]])
        {
            name =rep.username;
        }
        else
        {
            name = @"游客";
        }
        self.repUsername.attributedText = [self changetextFont:name WithFont:[NSString stringWithFormat:@"%@",rep.floor]];;
        self.repContent.text =rep.content;
        repConH = [self setRowHeight:rep.content];
        //NSLog(@"%@ --- %@ ",rep.username,rep.content);

    }
    
    
    //调整content高度
    //计算内容高度
    CGRect conFrame = self.content.frame;
    conFrame.size.height = conH;
    self.content.frame = conFrame;
    
    //计算内容里的内容高度
    CGRect repFrame = self.repContent.frame;
    repFrame.size.height = repConH;
    self.repContent.frame = repFrame;
    
    //计算repView frame
    CGRect repViewFrame = self.repView.frame;
    repViewFrame.origin.y = CGRectGetMaxY(conFrame) + 10;
    repViewFrame.size.height = 26 + repConH ;
    self.repView.frame = repViewFrame;
    
    _rowHeight = CGRectGetMaxY(self.content.frame) + repConH + 26;
    
    [accout getHeight:_rowHeight];
}

- (CGFloat)getRowHeight
{
    return _rowHeight;
}

- (CGFloat)setRowHeight:(NSString *)s
{
    NSDictionary *attrs = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
    CGRect frame = [s boundingRectWithSize:CGSizeMake(245, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil];
    return CGRectGetHeight(frame) ;
}

//时间格式
- (NSString *)intervalSinceNow: (NSString *) theDate
{
    
//    NSDateFormatter *date=[[NSDateFormatter alloc] init];
//    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSDate *d=[date dateFromString:theDate];
    
//    NSTimeInterval late=[d timeIntervalSince1970]*1;
    NSTimeInterval late = [theDate floatValue];
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now=[dat timeIntervalSince1970]*1;
    NSString *timeString=@"";
    
    NSTimeInterval cha=now-late;
    
    if (cha/3600<1) {
        timeString = [NSString stringWithFormat:@"%f", cha/60];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@分钟前", timeString];
        
    }
    if (cha/3600>1&&cha/86400<1) {
        timeString = [NSString stringWithFormat:@"%f", cha/3600];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@小时前", timeString];
    }
    if (cha/86400>1)
    {
        timeString = [NSString stringWithFormat:@"%f", cha/86400];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@天前", timeString];
        
    }
    return timeString;
}

- (NSMutableAttributedString *)changetextFont:(NSString *)str1 WithFont:(NSString *)str2
{
    NSInteger leng = [str1 length];
    NSString *str3 = [NSString stringWithFormat:@"%@ %@L",str1,str2];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:str3];
    
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, leng)];
    
    return str;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
